﻿using System;
using System.Linq;
using System.Collections.ObjectModel;
using CodeCounsel.SharePoint.RibbonDesigner.Model;
using System.Windows.Input;
using System.Windows;
using System.Collections.Generic;
using System.ComponentModel;

namespace CodeCounsel.SharePoint.RibbonDesigner.ViewModel
{
    public class RibbonViewModel
        : DomainViewModel<Ribbon>
    {
        RibbonTemplates _templates;
        Ribbon _ribbon;
        TabViewModelCollection _tabs;
        TabViewModel _selectedTab;
        public IEnumerable<TabViewModel> Tabs
        {
            get { return _tabs; }
        }

        public TabViewModel SelectedTab 
        {
            get { return _selectedTab; }
            set
            {
                if (_selectedTab != value)
                {
                    _selectedTab = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("SelectedTab"));
                }
            }
        }

        public RibbonViewModel(Ribbon ribbon, RibbonTemplates templates)
            : base(ribbon)
        {
            _templates = templates;
            _ribbon = ribbon;
            RecreateTabs(ribbon);
        }

        void RecreateTabs(Ribbon ribbon)
        {
            TabContainer tabs = ExtensionManager.GetReplacedChild<TabContainer>(ribbon);
            _tabs = new TabViewModelCollection(tabs, _templates);
            OnPropertyChanged(new PropertyChangedEventArgs("Tabs"));
        }
    }
}
